{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "from gs_quant.session import GsSession\n",
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(client_id=None, client_secret=None, scopes=('run_analytics',)) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Costless 1x2 Put Spreads \n",
    "\n",
    "This screen looks for max payouts and breakeven levels for 1x2 zero cost put spreads for a given tenor. Top strike is fixed at the specified delta and the bottom strike is solved to make the structure zero-cost."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.markets.portfolio import Portfolio\n",
    "from gs_quant.markets import PricingContext\n",
    "from gs_quant.instrument import FXOption\n",
    "from gs_quant.risk import FXSpot\n",
    "from gs_quant.common import BuySell\n",
    "import pandas as pd; pd.set_option('display.precision', 2)\n",
    "\n",
    "def get_screen(df_portfolio, strike_label='40d'):\n",
    "    screen = pd.pivot_table(df_portfolio, values='strike_price', index=['pair', 'Spot'], columns=['buy_sell'])\n",
    "    screen = screen.reset_index('Spot')\n",
    "    upper_label, lower_label = f'Upper Strike ({strike_label})', 'Lower Strike (Mid)*'\n",
    "    screen = screen.rename(columns={BuySell.Buy: upper_label, BuySell.Sell: lower_label})\n",
    "    screen['Max Payout'] = screen[upper_label] / screen[lower_label] - 1\n",
    "    screen['Lower Breakeven (Mid)'] = 2 * screen[lower_label] - screen[upper_label]\n",
    "    screen['Lower Breakeven OTMS'] = abs(screen['Lower Breakeven (Mid)'] / screen['Spot'] - 1)\n",
    "    return screen\n",
    "\n",
    "def calculate_bear_put_spreads(upper_strike='40d', crosses=['USDNOK'], tenor='3m'):\n",
    "    portfolio = Portfolio()\n",
    "    for cross in crosses:\n",
    "        option_type = 'Put' if cross[:3] == 'USD' else 'Call'\n",
    "        upper_leg = FXOption(pair=f'{cross}', strike_price=upper_strike, notional_amount=10e6, \n",
    "                             option_type=option_type, buy_sell=BuySell.Buy, expiration_date=tenor, premium_currency=cross[:3])\n",
    "        lower_leg = FXOption(pair=f'{cross}', strike_price=f'P={abs(upper_leg.premium)}', notional_amount=20e6,\n",
    "                             option_type=option_type, buy_sell=BuySell.Sell, expiration_date=tenor)\n",
    "        portfolio.append((upper_leg, lower_leg))\n",
    "    with PricingContext():\n",
    "        portfolio.resolve()\n",
    "        spot = portfolio.calc(FXSpot)\n",
    "    summary = portfolio.to_frame()\n",
    "    summary['Spot'] = list(spot.result())\n",
    "    return get_screen(summary, upper_strike)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "    #T_779e0606_72d3_11eb_b8f4_0050561420b8row0_col3 {\n",
       "            background-color:  #023858;\n",
       "            color:  #f1f1f1;\n",
       "        }    #T_779e0606_72d3_11eb_b8f4_0050561420b8row0_col5 {\n",
       "            background-color:  #023858;\n",
       "            color:  #f1f1f1;\n",
       "        }    #T_779e0606_72d3_11eb_b8f4_0050561420b8row1_col3 {\n",
       "            background-color:  #045382;\n",
       "            color:  #f1f1f1;\n",
       "        }    #T_779e0606_72d3_11eb_b8f4_0050561420b8row1_col5 {\n",
       "            background-color:  #5c9fc9;\n",
       "            color:  #000000;\n",
       "        }    #T_779e0606_72d3_11eb_b8f4_0050561420b8row2_col3 {\n",
       "            background-color:  #045788;\n",
       "            color:  #f1f1f1;\n",
       "        }    #T_779e0606_72d3_11eb_b8f4_0050561420b8row2_col5 {\n",
       "            background-color:  #9fbad9;\n",
       "            color:  #000000;\n",
       "        }    #T_779e0606_72d3_11eb_b8f4_0050561420b8row3_col3 {\n",
       "            background-color:  #046096;\n",
       "            color:  #f1f1f1;\n",
       "        }    #T_779e0606_72d3_11eb_b8f4_0050561420b8row3_col5 {\n",
       "            background-color:  #c4cbe3;\n",
       "            color:  #000000;\n",
       "        }    #T_779e0606_72d3_11eb_b8f4_0050561420b8row4_col3 {\n",
       "            background-color:  #04649d;\n",
       "            color:  #f1f1f1;\n",
       "        }    #T_779e0606_72d3_11eb_b8f4_0050561420b8row4_col5 {\n",
       "            background-color:  #ebe6f2;\n",
       "            color:  #000000;\n",
       "        }    #T_779e0606_72d3_11eb_b8f4_0050561420b8row5_col3 {\n",
       "            background-color:  #cacee5;\n",
       "            color:  #000000;\n",
       "        }    #T_779e0606_72d3_11eb_b8f4_0050561420b8row5_col5 {\n",
       "            background-color:  #fff7fb;\n",
       "            color:  #000000;\n",
       "        }    #T_779e0606_72d3_11eb_b8f4_0050561420b8row6_col3 {\n",
       "            background-color:  #d5d5e8;\n",
       "            color:  #000000;\n",
       "        }    #T_779e0606_72d3_11eb_b8f4_0050561420b8row6_col5 {\n",
       "            background-color:  #e0deed;\n",
       "            color:  #000000;\n",
       "        }    #T_779e0606_72d3_11eb_b8f4_0050561420b8row7_col3 {\n",
       "            background-color:  #f0eaf4;\n",
       "            color:  #000000;\n",
       "        }    #T_779e0606_72d3_11eb_b8f4_0050561420b8row7_col5 {\n",
       "            background-color:  #549cc7;\n",
       "            color:  #000000;\n",
       "        }    #T_779e0606_72d3_11eb_b8f4_0050561420b8row8_col3 {\n",
       "            background-color:  #fff7fb;\n",
       "            color:  #000000;\n",
       "        }    #T_779e0606_72d3_11eb_b8f4_0050561420b8row8_col5 {\n",
       "            background-color:  #034165;\n",
       "            color:  #f1f1f1;\n",
       "        }</style><table id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8\" ><thead>    <tr>        <th class=\"index_name level0\" >buy_sell</th>        <th class=\"col_heading level0 col0\" >Spot</th>        <th class=\"col_heading level0 col1\" >Upper Strike (40d)</th>        <th class=\"col_heading level0 col2\" >Lower Strike (Mid)*</th>        <th class=\"col_heading level0 col3\" >Max Payout</th>        <th class=\"col_heading level0 col4\" >Lower Breakeven (Mid)</th>        <th class=\"col_heading level0 col5\" >Lower Breakeven OTMS</th>    </tr>    <tr>        <th class=\"index_name level0\" >pair</th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>        <th class=\"blank\" ></th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8level0_row0\" class=\"row_heading level0 row0\" >USD NOK</th>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row0_col0\" class=\"data row0 col0\" >8.49</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row0_col1\" class=\"data row0 col1\" >8.37</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row0_col2\" class=\"data row0 col2\" >8.18</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row0_col3\" class=\"data row0 col3\" >2.29%</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row0_col4\" class=\"data row0 col4\" >7.99</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row0_col5\" class=\"data row0 col5\" >5.81%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8level0_row1\" class=\"row_heading level0 row1\" >USD SEK</th>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row1_col0\" class=\"data row1 col0\" >8.33</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row1_col1\" class=\"data row1 col1\" >8.24</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row1_col2\" class=\"data row1 col2\" >8.09</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row1_col3\" class=\"data row1 col3\" >1.82%</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row1_col4\" class=\"data row1 col4\" >7.94</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row1_col5\" class=\"data row1 col5\" >4.70%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8level0_row2\" class=\"row_heading level0 row2\" >USD JPY</th>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row2_col0\" class=\"data row2 col0\" >105.79</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row2_col1\" class=\"data row2 col1\" >104.84</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row2_col2\" class=\"data row2 col2\" >103.03</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row2_col3\" class=\"data row2 col3\" >1.75%</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row2_col4\" class=\"data row2 col4\" >101.23</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row2_col5\" class=\"data row2 col5\" >4.31%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8level0_row3\" class=\"row_heading level0 row3\" >USD CHF</th>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row3_col0\" class=\"data row3 col0\" >0.90</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row3_col1\" class=\"data row3 col1\" >0.89</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row3_col2\" class=\"data row3 col2\" >0.87</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row3_col3\" class=\"data row3 col3\" >1.55%</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row3_col4\" class=\"data row3 col4\" >0.86</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row3_col5\" class=\"data row3 col5\" >4.05%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8level0_row4\" class=\"row_heading level0 row4\" >USD CAD</th>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row4_col0\" class=\"data row4 col0\" >1.27</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row4_col1\" class=\"data row4 col1\" >1.26</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row4_col2\" class=\"data row4 col2\" >1.24</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row4_col3\" class=\"data row4 col3\" >1.45%</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row4_col4\" class=\"data row4 col4\" >1.22</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row4_col5\" class=\"data row4 col5\" >3.66%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8level0_row5\" class=\"row_heading level0 row5\" >NZD USD</th>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row5_col0\" class=\"data row5 col0\" >0.72</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row5_col1\" class=\"data row5 col1\" >0.73</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row5_col2\" class=\"data row5 col2\" >0.74</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row5_col3\" class=\"data row5 col3\" >-1.10%</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row5_col4\" class=\"data row5 col4\" >0.74</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row5_col5\" class=\"data row5 col5\" >3.34%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8level0_row6\" class=\"row_heading level0 row6\" >AUD USD</th>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row6_col0\" class=\"data row6 col0\" >0.77</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row6_col1\" class=\"data row6 col1\" >0.78</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row6_col2\" class=\"data row6 col2\" >0.79</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row6_col3\" class=\"data row6 col3\" >-1.28%</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row6_col4\" class=\"data row6 col4\" >0.80</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row6_col5\" class=\"data row6 col5\" >3.77%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8level0_row7\" class=\"row_heading level0 row7\" >EUR USD</th>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row7_col0\" class=\"data row7 col0\" >1.21</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row7_col1\" class=\"data row7 col1\" >1.22</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row7_col2\" class=\"data row7 col2\" >1.24</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row7_col3\" class=\"data row7 col3\" >-1.85%</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row7_col4\" class=\"data row7 col4\" >1.26</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row7_col5\" class=\"data row7 col5\" >4.73%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8level0_row8\" class=\"row_heading level0 row8\" >GBP USD</th>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row8_col0\" class=\"data row8 col0\" >1.39</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row8_col1\" class=\"data row8 col1\" >1.41</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row8_col2\" class=\"data row8 col2\" >1.44</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row8_col3\" class=\"data row8 col3\" >-2.33%</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row8_col4\" class=\"data row8 col4\" >1.47</td>\n",
       "                        <td id=\"T_779e0606_72d3_11eb_b8f4_0050561420b8row8_col5\" class=\"data row8 col5\" >5.74%</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x1f82a7eee48>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g10 = ['USDJPY', 'EURUSD', 'AUDUSD', 'GBPUSD', 'USDCAD', 'USDNOK', 'NZDUSD', 'USDSEK', 'USDCHF']\n",
    "\n",
    "result = calculate_bear_put_spreads(upper_strike='40d', crosses=g10, tenor='3m')\n",
    "result.sort_values(by='Max Payout', ascending=False).style.format({\n",
    "    'Max Payout': '{:,.2%}'.format, 'Lower Breakeven OTMS': '{:,.2%}'.format}).background_gradient(\n",
    "    subset=['Max Payout', 'Lower Breakeven OTMS'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}